#include <bits/stdc++.h>

using namespace std;

int main_60()
{
    int r, y, g, n, k, t;
    long long ans = 0, tmp;
    scanf("%d%d%d%d", &r, &y, &g, &n);
    while(n--) {
        scanf("%d%d", &k, &t);

        if(k != 0) {
            if(k == 1)
                tmp = ans + (r - t);
            else if(k == 2)
                 tmp = ans + r + g +y - t;
            else if(k == 3)
                tmp = ans + r + g - t;
            tmp %= r + g + y;

            if(tmp >= r + g)
                t = r + g + y + r - tmp;
            else if(tmp <= r)
                t = r - tmp;
            else
                t = 0;
        }
        ans += t;
    }

    printf("%lld", ans);

    return 0;
}
